package com.gcit.seniorWish.web.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gcit.seniorWish.core.util.Convert;
import com.gcit.seniorWish.web.common.persistence.dao.UserMapper;
import com.gcit.seniorWish.web.common.persistence.dao.UserRoleMapper;
import com.gcit.seniorWish.web.common.persistence.model.RoleMenu;
import com.gcit.seniorWish.web.common.persistence.model.User;
import com.gcit.seniorWish.web.common.persistence.model.UserRole;
import com.gcit.seniorWish.web.modular.system.service.IUserMgrService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserMgrServiceImpl extends ServiceImpl<UserMapper, User> implements IUserMgrService {

	@Resource
	private UserRoleMapper userRoleMapper;

	/**
	* 删除用户
	* @param userId 用户id
	* @author gaozhichang
	* @date 2018-03-14 上午 10:10
	*/
	@Override
	@Transactional
	public void deleteUser(String userId){
		//删除用户表内容
		baseMapper.deleteById(userId);
		//删除分配用户的角色
		userRoleMapper.delete(Condition.create().eq("user_id",userId));
	}

	/**
	* 设置用户的角色
	* @param userId 用户id
	* @param roleIds 角色id
	* @author gaozhichang
	* @date 2018-03-14 上午 10:30
	*/
	@Override
	@Transactional
	public void setRoles(String userId, String roleIds){
		// 删除该用户之前分配的角色
		userRoleMapper.delete(Condition.create().eq("user_id",userId));
		// 添加新的权限
		for (String id :  Convert.toStrArray(",", roleIds)) {
			UserRole relation = new UserRole();
			relation.setUserId(userId);
			relation.setRoleId(id);
			userRoleMapper.insert(relation);
		}
	}


	public void vaildUserExist(){

	}
}
